package com.nt.oam.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.nt.common.core.domain.entity.SysRole;
import com.nt.common.core.domain.entity.SysUser;
import com.nt.common.enums.YesOrNoEnum;
import com.nt.common.utils.DateUtils;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Objects;

import com.nt.common.utils.SecurityUtils;
import com.nt.common.utils.StringUtils;
import com.nt.common.utils.bean.BeanUtils;
import com.nt.oam.domain.*;
import com.nt.oam.vo.asset.DatabaseAssetVo;
import com.nt.system.service.impl.SysRoleServiceImpl;
import com.nt.system.service.impl.SysUserServiceImpl;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.nt.oam.mapper.AntDatabaseAssetMapper;
import com.nt.oam.service.IAntDatabaseAssetService;

/**
 * 数据库资产Service业务层处理
 *
 * @author JZFeng
 * @date 2025-02-05
 */
@Service
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
public class AntDatabaseAssetServiceImpl extends ServiceImpl<AntDatabaseAssetMapper, AntDatabaseAsset> implements IAntDatabaseAssetService {
    private final AntDatabaseAssetMapper antDatabaseAssetMapper;
    private final SysRoleServiceImpl sysRoleService;
    private final AntHospitalServiceImpl antHospitalService;
    private final AntHospitalProjectServiceImpl antHospitalProjectService;
    private final AntAssetCorrelationServiceImpl antAssetCorrelationService;

    private final SysUserServiceImpl sysUserService;


    /**
     * 查询数据库资产
     *
     * @param databaseAssetId 数据库资产主键
     * @return 数据库资产
     */
    @Override
    public AntDatabaseAsset selectAntDatabaseAssetByDatabaseAssetId(Long databaseAssetId) {
        return antDatabaseAssetMapper.selectAntDatabaseAssetByDatabaseAssetId(databaseAssetId);
    }

    /**
     * 查询数据库资产列表
     *
     * @param antDatabaseAsset 数据库资产
     * @return 数据库资产
     */
    @Override
    public List<AntDatabaseAsset> selectAntDatabaseAssetList(AntDatabaseAsset antDatabaseAsset) {
        return antDatabaseAssetMapper.selectAntDatabaseAssetList(antDatabaseAsset);
    }

    /**
     * 新增数据库资产
     *
     * @param antDatabaseAsset 数据库资产
     * @return 结果
     */
    @Override
    public int insertAntDatabaseAsset(AntDatabaseAsset antDatabaseAsset) {
        antDatabaseAsset.setCreateTime(DateUtils.getNowDate());
        // 医院的id
        // 医院的id
        Long hospitalId = antDatabaseAsset.getHospitalId();
        antDatabaseAsset.setHospitalId(hospitalId);
        // 查询医院的名称
        AntHospital antHospital = antHospitalService.getById(hospitalId);
        if (Objects.nonNull(antHospital) && StringUtils.isNotEmpty(antHospital.getHospitalName())) {
            antDatabaseAsset.setHospitalName(antHospital.getHospitalName());
        }
        // 项目id
        Long projectId = antDatabaseAsset.getProjectId();
        antDatabaseAsset.setProjectId(projectId);
        AntHospitalProject antHospitalProject = antHospitalProjectService.getById(projectId);
        if (Objects.nonNull(antHospitalProject) && StringUtils.isNotEmpty(antHospitalProject.getProjectName())) {
            antDatabaseAsset.setProjectName(antHospitalProject.getProjectName());
        }
        // 创建人
        // 创建人
        Long userId = SecurityUtils.getUserId();
        SysUser sysUser = sysUserService.selectUserById(userId);
        if (Objects.nonNull(sysUser) && StringUtils.isNotEmpty(sysUser.getUserName())) {
            antDatabaseAsset.setCreatePerson(sysUser.getUserName());
        }
        antDatabaseAsset.setIsDeleted(YesOrNoEnum.NO.getCode());
        antDatabaseAsset.setIsModify(YesOrNoEnum.NO.getCode());
        return antDatabaseAssetMapper.insertAntDatabaseAsset(antDatabaseAsset);
    }

    /**
     * 修改数据库资产
     *
     * @param antDatabaseAsset 数据库资产
     * @return 结果
     */
    @Override
    public int updateAntDatabaseAsset(AntDatabaseAsset antDatabaseAsset) {
        antDatabaseAsset.setUpdateTime(DateUtils.getNowDate());
        // 医院的id
        // 医院的id
        Long hospitalId = antDatabaseAsset.getHospitalId();
        antDatabaseAsset.setHospitalId(hospitalId);
        // 查询医院的名称
        AntHospital antHospital = antHospitalService.getById(hospitalId);
        if (Objects.nonNull(antHospital) && StringUtils.isNotEmpty(antHospital.getHospitalName())) {
            antDatabaseAsset.setHospitalName(antHospital.getHospitalName());
        }
        // 项目id
        Long projectId = antDatabaseAsset.getProjectId();
        antDatabaseAsset.setProjectId(projectId);
        AntHospitalProject antHospitalProject = antHospitalProjectService.getById(projectId);
        if (Objects.nonNull(antHospitalProject) && StringUtils.isNotEmpty(antHospitalProject.getProjectName())) {
            antDatabaseAsset.setProjectName(antHospitalProject.getProjectName());
        }
        // 更新人
        // 创建人
        Long userId = SecurityUtils.getUserId();
        SysUser sysUser = sysUserService.selectUserById(userId);
        if (Objects.nonNull(sysUser) && StringUtils.isNotEmpty(sysUser.getUserName())) {
            antDatabaseAsset.setUpdatePerson(sysUser.getUserName());
        }
        antDatabaseAsset.setIsModify(YesOrNoEnum.NO.getCode());
        return antDatabaseAssetMapper.updateAntDatabaseAsset(antDatabaseAsset);
    }

    /**
     * 批量删除数据库资产
     *
     * @param databaseAssetIds 需要删除的数据库资产主键
     * @return 结果
     */
    @Override
    public int deleteAntDatabaseAssetByDatabaseAssetIds(Long[] databaseAssetIds) {
        List<Long> databaseAssetIdsList = Arrays.asList(databaseAssetIds);
        databaseAssetIdsList.forEach(a -> {
            AntDatabaseAsset antDatabaseAsset = antDatabaseAssetMapper.selectAntDatabaseAssetByDatabaseAssetId(a);
            antDatabaseAsset.setIsDeleted(YesOrNoEnum.YES.getCode());
            antDatabaseAssetMapper.updateAntDatabaseAsset(antDatabaseAsset);
        });
        //antDatabaseAssetMapper.deleteAntDatabaseAssetByDatabaseAssetIds(databaseAssetIds);
        return YesOrNoEnum.YES.getCode();
    }

    /**
     * 删除数据库资产信息
     *
     * @param databaseAssetId 数据库资产主键
     * @return 结果
     */
    @Override
    public int deleteAntDatabaseAssetByDatabaseAssetId(Long databaseAssetId) {
        return antDatabaseAssetMapper.deleteAntDatabaseAssetByDatabaseAssetId(databaseAssetId);
    }

    /**
     * 非系统-----查询数据库资产
     *
     * @param databaseAssetId 数据库资产主键
     * @return 数据库资产
     */
    @Override
    public DatabaseAssetVo selectDatabaseAsset(Long databaseAssetId) {
        // 源对象
        AntDatabaseAsset antDatabaseAsset = antDatabaseAssetMapper.selectAntDatabaseAssetByDatabaseAssetId(databaseAssetId);
        //医院名称
        AntHospital antHospital = antHospitalService.selectAntHospitalByHospitalId(antDatabaseAsset.getHospitalId());
        if (Objects.nonNull(antHospital) && StringUtils.isNotEmpty(antHospital.getHospitalName())) {
            antDatabaseAsset.setHospitalName(antHospital.getHospitalName());
        }
        // 项目名称
        AntHospitalProject antHospitalProject = antHospitalProjectService.selectAntHospitalProjectByProjectId(antDatabaseAsset.getProjectId());
        if (Objects.nonNull(antHospitalProject) && StringUtils.isNotEmpty(antHospitalProject.getProjectName())) {
            antDatabaseAsset.setProjectName(antHospitalProject.getProjectName());
        }
        // 资产名称
        AntAssetCorrelation antAssetCorrelation = antAssetCorrelationService.selectAntAssetCorrelationByAssetId(antDatabaseAsset.getAssetId());
        if (Objects.nonNull(antAssetCorrelation) && StringUtils.isNotEmpty(antAssetCorrelation.getAssetName())) {
            antDatabaseAsset.setAssetName(antAssetCorrelation.getAssetName());
        }
        // 目标对象
        DatabaseAssetVo databaseAssetVo = new DatabaseAssetVo();
        // 赋值目标对象
        BeanUtils.copyProperties(antDatabaseAsset, databaseAssetVo);
        return databaseAssetVo;
    }

    /**
     * 通过项目id来查询数据库资产
     *
     * @return 数据库资产
     */
    @Override
    public List<DatabaseAssetVo> selectByProjectIdDatabaseAsset(Long projectId) {
        List<DatabaseAssetVo> databaseAssetVoList = new ArrayList<>();
        // 源对象
        List<AntDatabaseAsset> antDatabaseAssetList = antDatabaseAssetMapper.selectByProjectIdDatabaseAsset(projectId);
        if (Objects.nonNull(antDatabaseAssetList) && antDatabaseAssetList.size() > 0) {
            antDatabaseAssetList.forEach(antDatabaseAsset -> {
                //医院名称
                AntHospital antHospital = antHospitalService.selectAntHospitalByHospitalId(antDatabaseAsset.getHospitalId());
                if (Objects.nonNull(antHospital) && StringUtils.isNotEmpty(antHospital.getHospitalName())) {
                    antDatabaseAsset.setHospitalName(antHospital.getHospitalName());
                }
                // 项目名称
                AntHospitalProject antHospitalProject = antHospitalProjectService.selectAntHospitalProjectByProjectId(antDatabaseAsset.getProjectId());
                if (Objects.nonNull(antHospitalProject) && StringUtils.isNotEmpty(antHospitalProject.getProjectName())) {
                    antDatabaseAsset.setProjectName(antHospitalProject.getProjectName());
                }
                // 资产名称
                AntAssetCorrelation antAssetCorrelation = antAssetCorrelationService.selectAntAssetCorrelationByAssetId(antDatabaseAsset.getAssetId());
                if (Objects.nonNull(antAssetCorrelation) && StringUtils.isNotEmpty(antAssetCorrelation.getAssetName())) {
                    antDatabaseAsset.setAssetName(antAssetCorrelation.getAssetName());
                }
                // 目标对象
                DatabaseAssetVo databaseAssetVo = new DatabaseAssetVo();
                // 赋值目标对象
                BeanUtils.copyProperties(antDatabaseAsset, databaseAssetVo);
                databaseAssetVoList.add(databaseAssetVo);
            });
        }
        return databaseAssetVoList;
    }

    /**
     * 通过医院id来查询数据库资产
     *
     * @return 数据库资产
     */
    @Override
    public List<DatabaseAssetVo> selectByHospitalIdDatabaseAsset(Long hospitalId) {
        List<DatabaseAssetVo> databaseAssetVoList = new ArrayList<>();
        // 源对象
        List<AntDatabaseAsset> antDatabaseAssetList = antDatabaseAssetMapper.selectByHospitalIdDatabaseAsset(hospitalId);
        if (Objects.nonNull(antDatabaseAssetList) && antDatabaseAssetList.size() > 0) {
            antDatabaseAssetList.forEach(antDatabaseAsset -> {
                //医院名称
                AntHospital antHospital = antHospitalService.selectAntHospitalByHospitalId(antDatabaseAsset.getHospitalId());
                if (Objects.nonNull(antHospital) && StringUtils.isNotEmpty(antHospital.getHospitalName())) {
                    antDatabaseAsset.setHospitalName(antHospital.getHospitalName());
                }
                // 项目名称
                AntHospitalProject antHospitalProject = antHospitalProjectService.selectAntHospitalProjectByProjectId(antDatabaseAsset.getProjectId());
                if (Objects.nonNull(antHospitalProject) && StringUtils.isNotEmpty(antHospitalProject.getProjectName())) {
                    antDatabaseAsset.setProjectName(antHospitalProject.getProjectName());
                }
                // 资产名称
                AntAssetCorrelation antAssetCorrelation = antAssetCorrelationService.selectAntAssetCorrelationByAssetId(antDatabaseAsset.getAssetId());
                if (Objects.nonNull(antAssetCorrelation) && StringUtils.isNotEmpty(antAssetCorrelation.getAssetName())) {
                    antDatabaseAsset.setAssetName(antAssetCorrelation.getAssetName());
                }
                // 目标对象
                DatabaseAssetVo databaseAssetVo = new DatabaseAssetVo();
                // 赋值目标对象
                BeanUtils.copyProperties(antDatabaseAsset, databaseAssetVo);
                databaseAssetVoList.add(databaseAssetVo);
            });
        }
        return databaseAssetVoList;
    }

    /**
     * 查询是否在修改
     */
    public int selectDatabaseModify(Long databaseAssetId) {
        AntDatabaseAsset antDatabaseAsset = this.getById(databaseAssetId);
        int isModify = 0;
        if (Objects.nonNull(antDatabaseAsset) && Objects.nonNull(antDatabaseAsset.getIsModify())) {
            isModify = antDatabaseAsset.getIsModify();
        }
        return isModify;
    }

    /**
     * 重新设置修改状态
     */
    public int updateDatabaseModify(Long databaseAssetId, Integer isModify) {
        AntDatabaseAsset antDatabaseAsset = this.getById(databaseAssetId);
        if (Objects.nonNull(antDatabaseAsset)) {
            antDatabaseAsset.setIsModify(isModify);
        }
        return antDatabaseAssetMapper.updateAntDatabaseAsset(antDatabaseAsset);
    }
}
